package com.zktr.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zktr.bean.UsersBean;
import com.zktr.pojo.PageBean;
import com.zktr.pojo.Users;
import com.zktr.service.UserService;

/**
 * Servlet implementation class UserServlet
 */
@WebServlet("/UserServlet")
public class UserServlet extends BaseServlet {
	private static final long serialVersionUID = 1L;
	private UserService uService=new UserService();
       /**
        * 查询操作：
        */
	protected void findAllusers(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		int pc = getPc(request);
		int ps = 10;
		PageBean<UsersBean> pageBean =uService.findAllUsers(pc, ps);//传递pc, ps给Service，得到PageBean
		pageBean.setUrl(getUrl(request));
		request.getSession().setAttribute("pageBean",pageBean.getBeanList() );
		request.getSession().setAttribute("pb",pageBean);
		System.out.println(pageBean.getTp());
		System.out.println(pageBean.getBeanList().get(0));
		out.println("0");
	}
	 /**
     * 修改操作
     */
	protected void updateusers(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		PrintWriter out = response.getWriter();
		String mobilephone = request.getParameter("mobilephone");
		String username=request.getParameter("username");
		int rankpoints = Integer.parseInt(request.getParameter("rankpoints"));
		int  userid = Integer.parseInt(request.getParameter("id"));
		Users users=new Users();
		users.setUserid(userid);
		users.setUsername(username);
		users.setMobilephone(mobilephone);
		users.setRankpoints(rankpoints);
		int x=uService.updateUser(users);
		if(x>1) {
			out.println("0");
		}
	}
	/**
	 * 删除会员
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void deleteuser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		int userid=Integer.parseInt(request.getParameter("userid"));
		int x = uService.deleteorders(userid);
		if(x>0) {
			out.println("0"); 
		}
}
	
	/**
     * 详情操作
     */
	protected void findOneusers(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		PrintWriter out = response.getWriter();
		String userid =request.getParameter("userid");
		Users users=new Users();
		users.setUserid(Integer.valueOf(userid));
		//调用方法：
		UsersBean x=uService.findOneusers(users);
		request.getSession().setAttribute("user", x);
		if(x!=null) {
			out.println("0");
		}
	}

	 /**
     * 添加用户操作：
     */
	protected void addUsers(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		PrintWriter out = response.getWriter();
		Users users=new Users();
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String sex = request.getParameter("sex");
		String birthday = request.getParameter("birthday");
		String email = request.getParameter("email");
		String mobilephone = request.getParameter("mobilephone");
		String userrank = request.getParameter("userrank");
		//封装：
		users.setUsername(username);
		users.setUserrank(Check(userrank));
		users.setPassword(password);
		users.setSex(Boolean.parseBoolean(sex));
		users.setBirthday(exchang(birthday));//进行日期的转换：
		users.setEmail(email);
		users.setMobilephone(mobilephone);
		//调用方法：
		int x=uService.addUsers(users);
		if (x>0) {
			out.print("添加成功！");
		}
	}
	private Date exchang(String birthday) {
		SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
		Date date = null;
		try {
			date = format.parse(birthday);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return date;
	}
	/**
	 * 会员排行：积分排行：积分前10
	 */
	
	protected void dataUsers(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		PrintWriter out = response.getWriter();
		PageBean<UsersBean> pageBean =uService.findAllUsers();//传递pc, ps给Service，得到PageBean
		request.getSession().setAttribute("pageBean",pageBean.getBeanList() );
		if (pageBean!=null) {
			out.println("0");
		}
		
	}
	/**
	 * 检查类型
	 * @param userrank
	 * @return
	 */
	private int Check(String userrank) {
		if (userrank.equals("青铜")) {
			return 1;
		} else if (userrank.equals("白银")) {
			return 2;
		}else if (userrank.equals("黄金")) {
			return 3;
		}else if (userrank.equals("铂金")) {
			return 4;
		}else if (userrank.equals("钻石")) {
			return 5;
		}
		return 0;
	}
	/**
	 * 判断在数据库是否存在：
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void findName(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		String username = request.getParameter("username");
		//封装：
		Users users=new Users();
		users.setUsername(username);
		users=uService.findoneName(users);
		if (users!=null) {
			//封装错误信息：
			out.print("0");
			request.getSession().setAttribute("username",username);
			
		}
		
		//查找：
		
	}
	
	/**
	 *   得到当前页码：pc
	 *   如果pc参数不存在，默认pc=1
	 *   如果pc参数存在，需要转换成int类型即可
	 */
	private int getPc(HttpServletRequest request) {
		String value = request.getParameter("pc");
		if(value == null || value.trim().isEmpty()) {
			return 1;
		}
		try{
			return Integer.parseInt(value);
		}catch(Exception e){
			return  1;
		}
		
		
		
	}
	
	
	/**
	 * 截取url
	 *   /项目名/Servlet路径?参数字符串
	 * @param request
	 * @return
	 */
	private String getUrl(HttpServletRequest request) {
		String contextPath = request.getContextPath();//获取项目名
		String servletPath = request.getServletPath();//获取servletPath，即/CustomerServlet
		String queryString = request.getQueryString();//获取问号之后的参数部份
		
		//  判断参数部份中是否包含pc这个参数，如果包含，需要截取下去，不要这一部份。
		if(queryString.contains("&pc=")) {
			int index = queryString.lastIndexOf("&pc=");
			queryString = queryString.substring(0, index);
		}
		return contextPath + servletPath + "?" + queryString;
	}


}
